package com.hyb.hybmodulesfront.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hyb.hybmodulesfront.beans.vo.Activity;
import com.hyb.hybmodulesfront.beans.vo.IndexSchoolRole;
import com.hyb.hybmodulesfront.beans.vo.SchoolRole;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.*;
import com.ruoyi.system.api.domain.Signin;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    RemoteCarouselService remoteCarouselService;

    @Autowired
    RemoteActivityService remoteActivityService;

    @Autowired
    RemoteSchoolRoleService remoteSchoolRoleService;

    @Autowired
    RemoteSignupRuleService remoteSignupRuleService;

    @Autowired
    RemoteOssService remoteOssService;

    @Autowired
    RemoteSignupService remoteSignupService;

    @Autowired
    RemoteSigninService remoteSigninService;



    @GetMapping("/carousel")
    public AjaxResult carousel(){
        //获取热度展示前五的轮播数据
        AjaxResult ajaxResult = remoteCarouselService.listCarousel(SecurityConstants.INNER);
        if (ajaxResult.isError()){
            return AjaxResult.error("远程调用轮播图展示失败,请联系管理员处理");
        }
        return AjaxResult.success().put("list",ajaxResult.get("list"));
    }

    @GetMapping("/hotActivity")
    public AjaxResult hotActivity(){
        AjaxResult ajaxResult=remoteActivityService.indexHotCount(SecurityConstants.INNER);
        if (ajaxResult.isError()){
            return AjaxResult.error("远程调用活动服务失败,无法获取信息");
        }
        return AjaxResult.success().put("list",ajaxResult.get("list"));
    }

    @GetMapping("/info")
    public AjaxResult getActivityInfo(@RequestParam("id")String id){
        AjaxResult ajaxResult=remoteActivityService.getInfoByInnerAuth(Long.parseLong(id),SecurityConstants.INNER);
        if (ajaxResult.isError()){
            return AjaxResult.error("远程活动信息获取失败,请联系管理员处理");
        }
        return AjaxResult.success().put("activity",ajaxResult.get("activity"));
    }

    @GetMapping("/getActivityPerson")
    public AjaxResult getActivityPerson(@RequestParam("activityId") String activityId){

        Long id=Long.parseLong(activityId);

        //先把整个活动查出来
        AjaxResult infoByInnerAuth = remoteActivityService.getInfoByInnerAuth(id, SecurityConstants.INNER);

        Object activityMap =  infoByInnerAuth.get("activity");
        Activity activity = JSON.parseObject(JSON.toJSONString(activityMap), Activity.class);

        //先查负责人的信息
        AjaxResult innerInfo = remoteSchoolRoleService.getInnerInfo(activity.getUserId(), SecurityConstants.INNER);

        Object roleMap = innerInfo.get("role");
        IndexSchoolRole indexSchoolRole = JSONObject.parseObject(JSON.toJSONString(roleMap), IndexSchoolRole.class);


        String prepareField3 = activity.getPrepareField3();
        String[] split = prepareField3.split(",");

        List<IndexSchoolRole> list=new ArrayList<>();
        for (String s : split) {
            if(!StringUtils.isEmpty(s)){
                //查询组织者信息
                AjaxResult info = remoteSchoolRoleService.getInnerInfo(Long.parseLong(s), SecurityConstants.INNER);
                Object o = info.get("role");
                IndexSchoolRole indexSchoolRole1 = JSON.parseObject(JSON.toJSONString(o), IndexSchoolRole.class);
                list.add(indexSchoolRole1);
            }

        }
        //查询指导老师 TODO

        //查询所有报名者
        AjaxResult indexSchoolRoles = remoteSignupRuleService.getSignUpByActivityId(String.valueOf(id), SecurityConstants.INNER);

        Map<String,Object> map=new HashMap<>();
        map.put("main",indexSchoolRole);
        map.put("mainList",list);
        map.put("otherList",indexSchoolRoles.get("list"));

        return AjaxResult.success().put("map",map);

    }

    @GetMapping("/resource/video")
    public AjaxResult getVideos(@RequestParam("ids")String ids){

        if(StringUtils.isEmpty(ids)){
            return AjaxResult.error("该活动不存在视频资源");
        }

        String[] split = ids.split(",");
        List<Object> list=new ArrayList<>();
        for (String s : split) {
            String[] strings = s.split(":");
            AjaxResult video = remoteOssService.getVideo(strings[0], SecurityConstants.INNER);
            list.add(video.get("urls"));
        }
        return AjaxResult.success().put("list",list);
    }

    @GetMapping("/signUpInfo")
    public AjaxResult getSignUpInfo(@RequestParam("id")String id){
        if(StringUtils.isEmpty(id)){
            return AjaxResult.error("id不能为空");
        }
        AjaxResult personalSignUp = remoteSignupService.getPersonalSignUp(id, SecurityConstants.INNER);
        return AjaxResult.success().put("signUp",personalSignUp.get("signUp"));
    }

    @GetMapping("/getSignInInfo")
    public AjaxResult getSignInInfo(@RequestParam("activityId")String activityId){
        Signin signin=new Signin();
        signin.setUserId(SecurityUtils.getUserId());
        signin.setActivityId(Long.parseLong(activityId));
        AjaxResult info = remoteSigninService.getInfo(signin, SecurityConstants.INNER);
        return AjaxResult.success().put("info",info.get("signIn"));
    }
}
